package main.java.model;

import java.util.Observable;
import java.util.Random;

public class Gears extends Observable {
    
    //static final variables to sleep time
	private static final int DEFAULT_SLEEP = 1600;
    
    //static final variables to change positions id easier
    private static final int UP_ID = 0;
    private static final int MANEUVERING_ID = 1;
    private static final int DOWN_ID = 2;
    private static final int FAILURE_ID = 3;
    
    private int position, sleepUp, sleepDown;
    
    /**
     * Construction Gears
     */
    public Gears() {
        this.position = UP_ID;
        this.sleepUp = DEFAULT_SLEEP;
        this.sleepDown = DEFAULT_SLEEP;
    }

    /**
     * Construction Gears
     * @param sleepUp : Time configuration depending on which gear to go up
     * @param sleepDown : Time configuration depending on which gear to go down
     */
    public Gears(int sleepUp, int sleepDown) {
        this.position = UP_ID;
        this.sleepUp = sleepUp;
        this.sleepDown = sleepDown;
    }
    
    /**
     * Return the position in a string
     * @return String : the position in a string
     */
    public String positionToString() {
        switch (position) {
            case UP_ID :
                return "Up";
            case MANEUVERING_ID :
                return "Maneuvering";
            case DOWN_ID :
                return "Down";
            case FAILURE_ID :
                return "Failure";
            default :
                return "Default";
        }
    }
    
    /**
     * From the string value of the position
     * Get the number that matches
     * @param positionString : the string
     * @return int : the number
     */
    public int getPositionValue(String positionString) {
        switch (positionString) {
            case "Up" :
                return UP_ID;
            case "Maneuvering" :
                return MANEUVERING_ID;
            case "Down" :
                return DOWN_ID;
            case "Failure" :
                return FAILURE_ID;
            default :
                return -1;
        }
    }

    /**
     * Gears' action depending on the change that happened
     * @param position : the change
     */
    public void action(String position) {
        switch (position) {
            case "Closed":
                //Set to up 
                setPosition(UP_ID); 
                break;
            case "Down":
                //Gears go down
                maneuveringDown();
                break;
            case "High":
                //Gears go up
                maneuveringUp();
                break;
            default:
                break;
        }
    }
    
    /**
     * Maneuvering gears to down position
     */
    public void maneuveringDown() {
        //Set to maneuvering 
        setPosition(MANEUVERING_ID);
        try {
            //Sleep
            Thread.sleep(sleepDown); 
        } catch (InterruptedException e) {
        }
        //Set to down 
        setPosition(DOWN_ID); 
    }

    /**
     * Maneuvering gears to up position
     */
    public void maneuveringUp() {
        //Set to maneuvering 
        setPosition(MANEUVERING_ID);
        try {
            //Sleep
            Thread.sleep(sleepUp); 
        } catch (InterruptedException e) {
        }
        //Set to up 
        setPosition(UP_ID); 
    }

    public int getPosition() {
        return position;
    }

    /**
     * Set Position and notify the main application
     * @param position : the new position
     */
    public void setPosition(int position) {
        this.position = position;
        
        //Setting changes
        setChanged(); 
        //Notify
        notifyObservers(positionToString()); 
    }
}
